/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/* Import Protocol 1
 * JSON Representation of the Sector.
 */

ImportModules[1] = {
  ImportSector: function(code)
  {
    code = code.replace("\n", "");
    var imported = JSON.decode(code);

    if (!$chk(Global.CachedMaps[imported.map]))
      throw "Unknown Map";

    var name = imported.name;
    var map = imported.map;
    var tiles = Global.CachedMaps[map];
    var buildings = [];
    var planets = [];
    var starbases = [];
    var backgrounds = [];

    for (var i = 0; i < imported.buildings.length; i++)
    {
      var building = imported.buildings[i];
      var id = building["id"];
      buildings.push(new Building(
        id,
        tiles[id].X,
        tiles[id].Y,
        building["name"],
        building["owner"],
        building["level"]
      ));
    }

    for (var i = 0; i < imported.planets.length; i++)
    {
      var planet = imported.planets[i];
      var id = planet["id"];
      planets.push(new Planet(
        id,
        tiles[id].X,
        tiles[id].Y,
        planet["type"],
        planet["population"]
      ));
    }

    for (var i = 0; i < imported.starbases.length; i++)
    {
      var starbase = imported.starbases[i];
      var id = starbase["id"];
      starbases.push(new Starbase(
        id,
        tiles[id].X,
        tiles[id].Y,
        tiles[id].Type,
        starbase["owner"] == 225,
        starbase["owner"],
        starbase["population"]
      ));
    }

    return new Sector(name, map, tiles, buildings, planets, starbases, backgrounds);
  },
  ImportPricelist: function(code)
  {
    var pricelist = JSON.decode(code);
    return new Pricelist(pricelist["name"], pricelist["buy"], pricelist["sell"]);
  }
};